//-----------------------------------------------------------------------
// <copyright file="a_DomainDAOBase.cs" company="AACECOM">
//     Copyright (c) AACECOM. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ThoiTrang.Entities;

namespace ThoiTrang.Data
{    
    public abstract class a_DomainDAOBase
    {
        #region Common methods
        public virtual a_Domain Createa_DomainFromReader(IDataReader reader)
        {
            a_Domain item = new a_Domain();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("DomainID"))) item.DomainID = (int)reader["DomainID"];
                if (!reader.IsDBNull(reader.GetOrdinal("GroupID"))) item.GroupID = (int)reader["GroupID"];
                if (!reader.IsDBNull(reader.GetOrdinal("DomainName"))) item.DomainName = (string)reader["DomainName"];
                if (!reader.IsDBNull(reader.GetOrdinal("DomainAlias"))) item.DomainAlias = (string)reader["DomainAlias"];
                if (!reader.IsDBNull(reader.GetOrdinal("CompanyName"))) item.CompanyName = (string)reader["CompanyName"];
                if (!reader.IsDBNull(reader.GetOrdinal("Address"))) item.Address = (string)reader["Address"];
                if (!reader.IsDBNull(reader.GetOrdinal("City"))) item.City = (string)reader["City"];
                if (!reader.IsDBNull(reader.GetOrdinal("State"))) item.State = (string)reader["State"];
                if (!reader.IsDBNull(reader.GetOrdinal("Zipcode"))) item.Zipcode = (string)reader["Zipcode"];
                if (!reader.IsDBNull(reader.GetOrdinal("Country"))) item.Country = (string)reader["Country"];
                if (!reader.IsDBNull(reader.GetOrdinal("Industry"))) item.Industry = (string)reader["Industry"];
                if (!reader.IsDBNull(reader.GetOrdinal("McFirstName"))) item.McFirstName = (string)reader["McFirstName"];
                if (!reader.IsDBNull(reader.GetOrdinal("McLastName"))) item.McLastName = (string)reader["McLastName"];
                if (!reader.IsDBNull(reader.GetOrdinal("McTitle"))) item.McTitle = (string)reader["McTitle"];
                if (!reader.IsDBNull(reader.GetOrdinal("McPhone"))) item.McPhone = (string)reader["McPhone"];
                if (!reader.IsDBNull(reader.GetOrdinal("McExt"))) item.McExt = (string)reader["McExt"];
                if (!reader.IsDBNull(reader.GetOrdinal("McFax"))) item.McFax = (string)reader["McFax"];
                if (!reader.IsDBNull(reader.GetOrdinal("McEmail"))) item.McEmail = (string)reader["McEmail"];
                if (!reader.IsDBNull(reader.GetOrdinal("TcFirstName"))) item.TcFirstName = (string)reader["TcFirstName"];
                if (!reader.IsDBNull(reader.GetOrdinal("TcLastName"))) item.TcLastName = (string)reader["TcLastName"];
                if (!reader.IsDBNull(reader.GetOrdinal("TcTitle"))) item.TcTitle = (string)reader["TcTitle"];
                if (!reader.IsDBNull(reader.GetOrdinal("TcPhone"))) item.TcPhone = (string)reader["TcPhone"];
                if (!reader.IsDBNull(reader.GetOrdinal("TcExt"))) item.TcExt = (string)reader["TcExt"];
                if (!reader.IsDBNull(reader.GetOrdinal("TcEmail"))) item.TcEmail = (string)reader["TcEmail"];
                if (!reader.IsDBNull(reader.GetOrdinal("PaymentType"))) item.PaymentType = (string)reader["PaymentType"];
                if (!reader.IsDBNull(reader.GetOrdinal("BillingContact"))) item.BillingContact = (string)reader["BillingContact"];
                if (!reader.IsDBNull(reader.GetOrdinal("CardType"))) item.CardType = (string)reader["CardType"];
                if (!reader.IsDBNull(reader.GetOrdinal("CardNum"))) item.CardNum = (string)reader["CardNum"];
                if (!reader.IsDBNull(reader.GetOrdinal("ExpireMonth"))) item.ExpireMonth = (string)reader["ExpireMonth"];
                if (!reader.IsDBNull(reader.GetOrdinal("ExpireYear"))) item.ExpireYear = (string)reader["ExpireYear"];
                if (!reader.IsDBNull(reader.GetOrdinal("NameOnCard"))) item.NameOnCard = (string)reader["NameOnCard"];
                if (!reader.IsDBNull(reader.GetOrdinal("Source"))) item.Source = (string)reader["Source"];
                if (!reader.IsDBNull(reader.GetOrdinal("AgreeToMMSA"))) item.AgreeToMMSA = (bool)reader["AgreeToMMSA"];
                if (!reader.IsDBNull(reader.GetOrdinal("IP"))) item.IP = (string)reader["IP"];
                if (!reader.IsDBNull(reader.GetOrdinal("DateSign"))) item.DateSign = (DateTime)reader["DateSign"];
                if (!reader.IsDBNull(reader.GetOrdinal("DateActivate"))) item.DateActivate = (DateTime)reader["DateActivate"];
                if (!reader.IsDBNull(reader.GetOrdinal("QtySoldToDate"))) item.QtySoldToDate = (decimal)reader["QtySoldToDate"];
                if (!reader.IsDBNull(reader.GetOrdinal("AmtSoldToDate"))) item.AmtSoldToDate = (decimal)reader["AmtSoldToDate"];
                if (!reader.IsDBNull(reader.GetOrdinal("TotalUsers"))) item.TotalUsers = (int)reader["TotalUsers"];
                if (!reader.IsDBNull(reader.GetOrdinal("TotalProducts"))) item.TotalProducts = (int)reader["TotalProducts"];
                if (!reader.IsDBNull(reader.GetOrdinal("Status"))) item.Status = (byte)reader["Status"];
            }
            catch (Exception ex)
            {                
                throw ex;
            }
            return item;
        }
        #endregion
        
        #region Createa_Domain methods
            
        public virtual void Createa_Domain(a_Domain a_Domain)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Domains_ins");
                
                database.AddInParameter(dbCommand, "@GroupID", DbType.Int32, a_Domain.GroupID);
                database.AddInParameter(dbCommand, "@DomainName", DbType.AnsiString, a_Domain.DomainName);
                database.AddInParameter(dbCommand, "@DomainAlias", DbType.AnsiString, a_Domain.DomainAlias);
                database.AddInParameter(dbCommand, "@CompanyName", DbType.AnsiString, a_Domain.CompanyName);
                database.AddInParameter(dbCommand, "@Address", DbType.AnsiString, a_Domain.Address);
                database.AddInParameter(dbCommand, "@City", DbType.AnsiString, a_Domain.City);
                database.AddInParameter(dbCommand, "@State", DbType.AnsiString, a_Domain.State);
                database.AddInParameter(dbCommand, "@Zipcode", DbType.AnsiString, a_Domain.Zipcode);
                database.AddInParameter(dbCommand, "@Country", DbType.AnsiString, a_Domain.Country);
                database.AddInParameter(dbCommand, "@Industry", DbType.AnsiString, a_Domain.Industry);
                database.AddInParameter(dbCommand, "@McFirstName", DbType.AnsiString, a_Domain.McFirstName);
                database.AddInParameter(dbCommand, "@McLastName", DbType.AnsiString, a_Domain.McLastName);
                database.AddInParameter(dbCommand, "@McTitle", DbType.AnsiString, a_Domain.McTitle);
                database.AddInParameter(dbCommand, "@McPhone", DbType.AnsiString, a_Domain.McPhone);
                database.AddInParameter(dbCommand, "@McExt", DbType.AnsiString, a_Domain.McExt);
                database.AddInParameter(dbCommand, "@McFax", DbType.AnsiString, a_Domain.McFax);
                database.AddInParameter(dbCommand, "@McEmail", DbType.AnsiString, a_Domain.McEmail);
                database.AddInParameter(dbCommand, "@TcFirstName", DbType.AnsiString, a_Domain.TcFirstName);
                database.AddInParameter(dbCommand, "@TcLastName", DbType.AnsiString, a_Domain.TcLastName);
                database.AddInParameter(dbCommand, "@TcTitle", DbType.AnsiString, a_Domain.TcTitle);
                database.AddInParameter(dbCommand, "@TcPhone", DbType.AnsiString, a_Domain.TcPhone);
                database.AddInParameter(dbCommand, "@TcExt", DbType.AnsiString, a_Domain.TcExt);
                database.AddInParameter(dbCommand, "@TcEmail", DbType.AnsiString, a_Domain.TcEmail);
                database.AddInParameter(dbCommand, "@PaymentType", DbType.AnsiString, a_Domain.PaymentType);
                database.AddInParameter(dbCommand, "@BillingContact", DbType.AnsiString, a_Domain.BillingContact);
                database.AddInParameter(dbCommand, "@CardType", DbType.AnsiString, a_Domain.CardType);
                database.AddInParameter(dbCommand, "@CardNum", DbType.AnsiString, a_Domain.CardNum);
                database.AddInParameter(dbCommand, "@ExpireMonth", DbType.AnsiString, a_Domain.ExpireMonth);
                database.AddInParameter(dbCommand, "@ExpireYear", DbType.AnsiString, a_Domain.ExpireYear);
                database.AddInParameter(dbCommand, "@NameOnCard", DbType.AnsiString, a_Domain.NameOnCard);
                database.AddInParameter(dbCommand, "@Source", DbType.AnsiString, a_Domain.Source);
                database.AddInParameter(dbCommand, "@AgreeToMMSA", DbType.Boolean, a_Domain.AgreeToMMSA);
                database.AddInParameter(dbCommand, "@IP", DbType.AnsiString, a_Domain.IP);
                database.AddInParameter(dbCommand, "@DateSign", DbType.DateTime, a_Domain.DateSign);
                database.AddInParameter(dbCommand, "@DateActivate", DbType.DateTime, a_Domain.DateActivate);
                database.AddInParameter(dbCommand, "@QtySoldToDate", DbType.Currency, a_Domain.QtySoldToDate);
                database.AddInParameter(dbCommand, "@AmtSoldToDate", DbType.Currency, a_Domain.AmtSoldToDate);
                database.AddInParameter(dbCommand, "@TotalUsers", DbType.Int32, a_Domain.TotalUsers);
                database.AddInParameter(dbCommand, "@TotalProducts", DbType.Int32, a_Domain.TotalProducts);
                database.AddInParameter(dbCommand, "@Status", DbType.Byte, a_Domain.Status);
                database.AddOutParameter(dbCommand, "@DomainID", DbType.Int32, 0);
                
                database.ExecuteNonQuery(dbCommand);
                a_Domain.DomainID = (int)database.GetParameterValue(dbCommand, "@DomainID");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Updatea_Domain methods
        
        public virtual void Updatea_Domain(a_Domain a_Domain)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Domains_upd");            
                
                database.AddInParameter(dbCommand, "@DomainID", DbType.Int32, a_Domain.DomainID);
                database.AddInParameter(dbCommand, "@GroupID", DbType.Int32, a_Domain.GroupID);
                database.AddInParameter(dbCommand, "@DomainName", DbType.AnsiString, a_Domain.DomainName);
                database.AddInParameter(dbCommand, "@DomainAlias", DbType.AnsiString, a_Domain.DomainAlias);
                database.AddInParameter(dbCommand, "@CompanyName", DbType.AnsiString, a_Domain.CompanyName);
                database.AddInParameter(dbCommand, "@Address", DbType.AnsiString, a_Domain.Address);
                database.AddInParameter(dbCommand, "@City", DbType.AnsiString, a_Domain.City);
                database.AddInParameter(dbCommand, "@State", DbType.AnsiString, a_Domain.State);
                database.AddInParameter(dbCommand, "@Zipcode", DbType.AnsiString, a_Domain.Zipcode);
                database.AddInParameter(dbCommand, "@Country", DbType.AnsiString, a_Domain.Country);
                database.AddInParameter(dbCommand, "@Industry", DbType.AnsiString, a_Domain.Industry);
                database.AddInParameter(dbCommand, "@McFirstName", DbType.AnsiString, a_Domain.McFirstName);
                database.AddInParameter(dbCommand, "@McLastName", DbType.AnsiString, a_Domain.McLastName);
                database.AddInParameter(dbCommand, "@McTitle", DbType.AnsiString, a_Domain.McTitle);
                database.AddInParameter(dbCommand, "@McPhone", DbType.AnsiString, a_Domain.McPhone);
                database.AddInParameter(dbCommand, "@McExt", DbType.AnsiString, a_Domain.McExt);
                database.AddInParameter(dbCommand, "@McFax", DbType.AnsiString, a_Domain.McFax);
                database.AddInParameter(dbCommand, "@McEmail", DbType.AnsiString, a_Domain.McEmail);
                database.AddInParameter(dbCommand, "@TcFirstName", DbType.AnsiString, a_Domain.TcFirstName);
                database.AddInParameter(dbCommand, "@TcLastName", DbType.AnsiString, a_Domain.TcLastName);
                database.AddInParameter(dbCommand, "@TcTitle", DbType.AnsiString, a_Domain.TcTitle);
                database.AddInParameter(dbCommand, "@TcPhone", DbType.AnsiString, a_Domain.TcPhone);
                database.AddInParameter(dbCommand, "@TcExt", DbType.AnsiString, a_Domain.TcExt);
                database.AddInParameter(dbCommand, "@TcEmail", DbType.AnsiString, a_Domain.TcEmail);
                database.AddInParameter(dbCommand, "@PaymentType", DbType.AnsiString, a_Domain.PaymentType);
                database.AddInParameter(dbCommand, "@BillingContact", DbType.AnsiString, a_Domain.BillingContact);
                database.AddInParameter(dbCommand, "@CardType", DbType.AnsiString, a_Domain.CardType);
                database.AddInParameter(dbCommand, "@CardNum", DbType.AnsiString, a_Domain.CardNum);
                database.AddInParameter(dbCommand, "@ExpireMonth", DbType.AnsiString, a_Domain.ExpireMonth);
                database.AddInParameter(dbCommand, "@ExpireYear", DbType.AnsiString, a_Domain.ExpireYear);
                database.AddInParameter(dbCommand, "@NameOnCard", DbType.AnsiString, a_Domain.NameOnCard);
                database.AddInParameter(dbCommand, "@Source", DbType.AnsiString, a_Domain.Source);
                database.AddInParameter(dbCommand, "@AgreeToMMSA", DbType.Boolean, a_Domain.AgreeToMMSA);
                database.AddInParameter(dbCommand, "@IP", DbType.AnsiString, a_Domain.IP);
                database.AddInParameter(dbCommand, "@DateSign", DbType.DateTime, a_Domain.DateSign);
                database.AddInParameter(dbCommand, "@DateActivate", DbType.DateTime, a_Domain.DateActivate);
                database.AddInParameter(dbCommand, "@QtySoldToDate", DbType.Currency, a_Domain.QtySoldToDate);
                database.AddInParameter(dbCommand, "@AmtSoldToDate", DbType.Currency, a_Domain.AmtSoldToDate);
                database.AddInParameter(dbCommand, "@TotalUsers", DbType.Int32, a_Domain.TotalUsers);
                database.AddInParameter(dbCommand, "@TotalProducts", DbType.Int32, a_Domain.TotalProducts);
                database.AddInParameter(dbCommand, "@Status", DbType.Byte, a_Domain.Status);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }
        
        #endregion

        #region Deletea_Domain methods
        public virtual void Deletea_Domain(int domainID)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Domains_del");
                
                database.AddInParameter(dbCommand, "@DomainID", DbType.Int32, domainID);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }

        #endregion

        #region Geta_Domain methods
        
        public virtual a_Domain Geta_Domain(int domainID)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Domains_sel");
                
                database.AddInParameter(dbCommand, "@DomainID", DbType.Int32, domainID);
                
                a_Domain a_Domain =  null;
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    if (reader.Read())
                    {
                        a_Domain = Createa_DomainFromReader(reader);
                        reader.Close();
                    }
                }
                return a_Domain;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        #endregion

        #region Geta_DomainList methods
        public virtual a_DomainCollection Geta_DomainList(a_DomainColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Domains_selList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                a_DomainCollection a_DomainCollection = new a_DomainCollection();
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        a_Domain a_Domain = Createa_DomainFromReader(reader);
                        a_DomainCollection.Add(a_Domain);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return a_DomainCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        public virtual a_DomainCollection Geta_DomainList(a_DomainColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return Geta_DomainList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
    }
}